package com.muchfish.reactor.api;

import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;

public class FluxSinkBackpressureExample {
    public static void main(String[] args) {
        Flux.create(fluxSink -> {
                    for (int i = 0; i < 1000; i++) {
                        fluxSink.next(i);
                    }
                }, FluxSink.OverflowStrategy.BUFFER) // 指定背压策略
                .onBackpressureBuffer(10, buffer -> System.err.println("Buffer overflow! Discarding: " + buffer))
                .subscribe(value -> {
                    // 模拟慢速消费者
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(value);
                });
    }
}
